<?php
namespace App\Http\Controllers\Admin;

use App\Models\CompanyInfo;
use App\Models\MemberOther;
use App\Models\NewAdmin;
use App\Models\Suggest;
use App\Models\SuggestLog;
use App\Models\SuggestType;
use App\Services\CommonService;
use Illuminate\Http\Request;

//投诉列表
class ComplaintController extends BaseController
{
    //获取投诉类型 http://shop.cn/Admin/Complaint/getSuggestType
    public function getSuggestType()
    {
        $suggestType = SuggestType::GetSuggestType();
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $suggestType);
    }

    //投诉列表POST
    public function complaintList(Request $request)
    {
        $input = $request->input();
        $where = [];
        if (!empty($input['work_order'])) {
            $where[] = ['work_order', 'like', '%' . trim($input['work_order']) . '%'];
        }
        if (!empty($input['suggest_type'])) {
            $where[] = ['suggest_type', $input['suggest_type']];
        }
        if (!empty($input['status'])) {
            $where[] = ['status', $input['status']];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', $time['begin_time']];
            $where[] = ['create_time', '<', $time['end_time']];
        }
        //工单等级：1普通 2紧急 3严重
        switch ($input['grade']) {
            case '1'://普通
                $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_ORDINARY];
                break;
            case '2'://紧急
                $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_URGENT];
                break;
            case '3'://严重
                $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_SERIOUS];
                break;

            default://全部
                $grade_where = [];
                break;
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = Suggest::GetComplaintList($pageSize, $where, $grade_where);
        $data = formatPage($data);
        //计算数量
        unset($grade_where);
        $data['count']['grade_total'] = Suggest::GetComplaintGradeCount($where, []);
        unset($grade_where);
        $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_ORDINARY];
        $data['count']['ordinary_grade_total'] = Suggest::GetComplaintGradeCount($where, $grade_where);
        unset($grade_where);
        $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_URGENT];
        $data['count']['urgent_grade_total'] = Suggest::GetComplaintGradeCount($where, $grade_where);
        unset($grade_where);
        $grade_where[] = ['grade', Suggest::SUGGEST_GRADE_SERIOUS];
        $data['count']['serious_grade_total'] = Suggest::GetComplaintGradeCount($where, $grade_where);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //查看 http://shop.cn/Admin/Complaint/look?id=
    public function look(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $suggest = Suggest::GetOneSuggest(array('id' => $input['id']));
        $suggestLog = SuggestLog::GetSuggestLog(array('suggest_id' => $input['id']),array());
        foreach ($suggestLog as $k => $v) {
            $applet_handle_typearr = array(
                SuggestLog::SUGGEST_LOG_HANDLE_TYPE_APPLY_COMPLAINT,
                SuggestLog::SUGGEST_LOG_HANDLE_TYPE_UNRESOLVED_COMPLAINT,
                SuggestLog::SUGGEST_LOG_HANDLE_TYPE_CANCEL_COMPLAINT,
                SuggestLog::SUGGEST_LOG_HANDLE_TYPE_RESOLVED_COMPLAINT,
            );
            if ($v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_PLATFORM_INTERVENE || $v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_REMARKS) {
                if ($v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_PLATFORM_INTERVENE) {
                    $suggestLog[$k]['operator_content'] = $v['handle_title'] . '：' . $v['content'];
                } else {
                    $suggestLog[$k]['operator_content'] = '客服备注内容：' . $v['content'];
                }
                $other_admin = NewAdmin::where(array('id' => $v['user_id']))->first();
                $other_admin = $other_admin ? $other_admin->toArray() : [];
                $suggestLog[$k]['operator'] = $other_admin['username'] . $other_admin['mobile'];
            } elseif (in_array($v['handle_type'], $applet_handle_typearr)) {
                if ($v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_APPLY_COMPLAINT) {
                    $suggestLog[$k]['operator_content'] = $v['handle_title'];
                } else {
                    $suggestLog[$k]['operator_content'] = $v['handle_title'] . '，' . $v['content'];
                }
                $member = MemberOther::where(array('id' => $v['user_id']))->first();
                $member = $member ? $member->toArray() : [];
                $cert_name = isset($member['cert_name'])?$member['cert_name']:'';
                $mobile = isset($member['mobile'])?$member['mobile']:'';
                $suggestLog[$k]['operator'] = $cert_name . $mobile;
            } elseif ($v['handle_type'] == SuggestLog::SUGGEST_LOG_HANDLE_TYPE_OVERTIME_COMPLAINT) {
                $suggestLog[$k]['operator_content'] = $v['handle_title'] . '，' . $v['content'];
                $suggestLog[$k]['operator'] = '系统';
            } else {
                $suggestLog[$k]['operator'] = '无';
            }
        }
        $data = array('suggest' => $suggest, 'suggestLog' => $suggestLog);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //平台介入POST http://shop.cn/Admin/Complaint/intervene  {"suggest_id":"","content":""}
    public function intervene(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id']) || empty($input['content'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $other_admin = $this->users; //用户信息
        $suggest = Suggest::GetOneSuggest(array('id' => $input['suggest_id']));
        if ($suggest['status'] != Suggest::SUGGEST_STATUS_NO_HANDLE && $suggest['status'] != Suggest::SUGGEST_STATUS_HANDLING) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '该工单状态不是待处理或者处理中');
        }
        $edit_res = Suggest::where(array('id' => $input['suggest_id']))->update(array('status' => Suggest::SUGGEST_STATUS_HANDLING, 'update_time' => date('Y-m-d H:i:s')));
        if (!$edit_res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作失败');
        } else {
            $suggestLogCount = SuggestLog::where(array('suggest_id' => $input['suggest_id'], 'handle_type' => SuggestLog::SUGGEST_LOG_HANDLE_TYPE_PLATFORM_INTERVENE))->count();
            if ($suggestLogCount == 0) {
                $title = '平台介入处理';
            } else {
                $number = $suggestLogCount + 1;
                $title = '平台' . $number . '次介入处理';
            }
            $res = SuggestLog::addData($input['suggest_id'], $other_admin['id'], $title, $input['content'], SuggestLog::SUGGEST_LOG_HANDLE_TYPE_PLATFORM_INTERVENE);
            if ($res) {
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '操作成功');
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '操作失败');
            }
        }
    }

    //客服备注记录 http://shop.cn/Admin/Complaint/remarksRecord?id=
    public function remarksRecord(Request $request)
    {
        $input = $request->input();
        if (empty($input['id'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, 'id为空');
        }
        $suggestLog = SuggestLog::GetSuggestLog(array('suggest_id' => $input['id'], 'handle_type' => SuggestLog::SUGGEST_LOG_HANDLE_TYPE_REMARKS),array());
        foreach ($suggestLog as $k => $v) {
            $suggestLog[$k]['operator_content'] = $v['content'];
            $other_admin = NewAdmin::where(array('id' => $v['user_id']))->first();
            $other_admin = $other_admin ? $other_admin->toArray() : [];
            $suggestLog[$k]['operator'] = $other_admin['username'] . $other_admin['mobile'];
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $suggestLog);
    }

    //添加客服备注POST http://shop.cn/Admin/Complaint/addRemarks  {"suggest_id":"","content":""}
    public function addRemarks(Request $request)
    {
        $input = $request->input();
        if (empty($input['suggest_id']) || empty($input['content'])) {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '缺少参数');
        }
        $other_admin = $this->users; //用户信息
        $res = SuggestLog::addData($input['suggest_id'], $other_admin['id'], '', $input['content'], SuggestLog::SUGGEST_LOG_HANDLE_TYPE_REMARKS);
        if ($res) {
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '添加成功');
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加失败');
        }
    }

    /**
     * 获取留言信息
     */
    public function getLeavingAMessage(Request $request){
        $user_name = $request->input('user_name','');
        $mobile = $request->input('mobile_phone','');
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',CommonService::PAGESIZE);
        $where = [];
        if(!empty($user_name)){
            $where[] = ['name',$user_name];
        }
        if(!empty($mobile)){
            $where[] = ['mobile_phone',$mobile];
        }
        $list = CompanyInfo::where($where)->orderBy('id','desc')->paginate($pageSize);
        $list = obToArray($list);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功',$list);
    }


}
